<template>
  <div :class="'main-wrap' + currentMonth">
    <div class="inner-wrap">
      <div class="row">
        <div class="day-title" v-for="x in 7" :key="'title' + x">{{ ['一', '二', '三', '四', '五', '六', '日'][x - 1] }}</div>
      </div>
      <div class="row" v-for="(row, inxr) in dayMap" :key="inxr">
        <template v-for="(col, inxc) in row">
          <div v-if="inxc > 0" class="day" :key="inxc" :style="'background: ' + getStyle(col)">
            <div v-if="col && (col == 1 || col == lastDay || col == dayOfNow)" class="tag-day">
              {{ col }}
            </div>
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<script>
import dayjs from 'dayjs'

export default {
  name: 'MiniCalendar',
  data() {
    return {
      dayMap: [],
      dayOfNow: -1,
      lastDay: -1,
      currentMonth: '',
    }
  },
  props: {},
  created() {
    window.dd = dayjs
  },
  mounted() {
    this.init()
  },
  computed: {},
  methods: {
    init() {
      const t = dayjs()
      const currentMonth = t.$M + 1
      const date = `${t.$y}-${currentMonth}-1`
      const dayOfNow = t.$D
      const wek = dayjs(date).day()
      const daysInMonth = dayjs(date).daysInMonth()
      let row = 1
      let col = wek == 0 ? 7 : wek
      const dayMap = []
      for (let i = 1; i <= daysInMonth; i++) {
        if (!dayMap[row]) dayMap[row] = []
        dayMap[row][col] = i
        if (col == 7) {
          col = 0
          row++
        }
        col++
      }
      this.currentMonth = `${currentMonth}`
      this.dayOfNow = dayOfNow
      this.lastDay = daysInMonth
      this.dayMap = dayMap
      // console.log('今天是  ', currentMonth, '月')
      // console.log('今天是  ', dayOfNow, '号')
      // console.log('本月1号  ', date)
      // console.log('本月1号 是星期  ', wek)
      // console.log('本月有  ', daysInMonth, '天')
      // console.log('本月日历', dayMap)
    },
    getStyle(c) {
      if (!c) return '#EEE'
      if (c == 1 || c == this.lastDay) return '#FFF'
      if (c > this.dayOfNow) return '#CCC'
      if (c == this.dayOfNow) return '#FFF;color:red'
      return '#80cbc4'
    },
  },
}
</script>

<style lang="scss" scoped>
[attribute^='.main-wrap'] {
  padding: 6px;
}

.main-wrap1::before {
  content: '1';
}
.main-wrap2::before {
  content: '2';
}
.main-wrap3::before {
  content: '3';
}
.main-wrap4::before {
  content: '4';
}
.main-wrap5::before {
  content: '5';
}
.main-wrap6::before {
  content: '6';
}
.main-wrap7::before {
  content: '7';
}
.main-wrap8::before {
  content: '8';
}
.main-wrap9::before {
  content: '9';
}
.main-wrap10::before {
  content: '10';
}
.main-wrap11::before {
  content: '11';
}
.main-wrap12::before {
  content: '12';
}
.main-wrap1::before,
.main-wrap2::before,
.main-wrap3::before,
.main-wrap4::before,
.main-wrap5::before,
.main-wrap6::before,
.main-wrap7::before,
.main-wrap8::before,
.main-wrap9::before,
.main-wrap10::before,
.main-wrap11::before,
.main-wrap12::before {
  position: absolute;
  color: #fff;
  opacity: 0.5;
  text-shadow: -1px 0 2px black, 0 1px 2px black, 1px 0 2px black, 0 -1px 2px black;
  top: calc(50% + 6px);
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
  font-size: 40px;
  font-weight: bold;
}

.inner-wrap {
  display: flex;
  flex-direction: column;
}
.row {
  display: flex;
  align-items: center;
  margin-bottom: 2px;
}

.day-title {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 12px;
  height: 10px;
  font-size: 12px;
  transform: scale(0.8);
  background: none;
  color: #eee;
}
.day-title + .day-title {
  margin-left: 2px;
}

.tag-day {
  transform: scale(0.8);
}
.day {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 12px;
  height: 10px;
  border-radius: 3px;
  background: #ddd;
}
.day + .day {
  margin-left: 2px;
}
</style>
